package com.ysd.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.ysd.Dao.Impl.BannerDaoImpl;
import com.ysd.Dao.Impl.NewsClassIficationDaoImpl;
import com.ysd.Dao.Impl.ProductSeriesDaoImpl;
import com.ysd.Dao.Impl.UserDaoImpl;
import com.ysd.entity.Banner;
import com.ysd.entity.NewsClassiIfication;
import com.ysd.entity.ProductSeries;
import com.ysd.util.BaseServlet;
import com.ysd.util.Result;


public class AdminBannerServlet extends BaseServlet {
	/**
	 * 分页查询轮播图
	 * 地址:adminBanner?reqName=getBanner
	 * @author 86176
	 *
	 */
	public void getBanner(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");//设置响应编码格式
		PrintWriter out = response.getWriter();
		
		BannerDaoImpl bDao = new BannerDaoImpl();//创建轮播实体类
		int page = Integer.parseInt(request.getParameter("page"));
		int size = Integer.parseInt(request.getParameter("size"));
		List<Banner> banner = bDao.getBannerByPage((page-1)*size,size);//调用分页轮播实体类
		
		Gson gson= new Gson();
		String res = gson.toJson(banner);//将banner转化为json字符串
		out.print(res);//响应数据
		
		
		out.flush();
		out.close();
		
	}
	
	/**
	 * 查询所有轮播图数量
	 * 地址:adminBanner?reqName=getAllBanner
	 * @author 86176
	 *
	 */
		public void getAllBanner(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			response.setCharacterEncoding("UTF-8");//设置响应编码格式
			PrintWriter out = response.getWriter();
			BannerDaoImpl bDao = new BannerDaoImpl();//创建轮播实体类
			List<Banner> banner = bDao.getBanner();//调用轮播实体类
			
			Gson gson= new Gson();
			String res = gson.toJson(banner.size());//将banner转化为json字符串
			out.print(res);//响应数据
			
			
			out.flush();
			out.close();
			
		}
		
	
	/**
	 * 删除指定轮播图
	 * localhost:8080/zhubaosheji/adminBanner?reqName=deleteBanner
	 * @author 86176
	 *
	 */
		public void deleteBanner(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			int bId = Integer.parseInt(request.getParameter("bannerId"));
			BannerDaoImpl bDao = new BannerDaoImpl();//创建轮播实体类
			int n = bDao.deleteBannerById(bId);//调用删除方法
			
			response.setCharacterEncoding("UTF-8");
			PrintWriter out = response.getWriter();
			
			if (n>0) {//删除成功
				out.print(Result.toClient(true, "删除成功",""));//{"success":true,"message":"删除成功","remark":""}	
			} else {//删除失败
				out.print(Result.toClient(false, "删除失败",""));//{"success":false,"message":"删除失败","remark":""}	
			}
			
			out.flush();
			out.close();
		}
		
		/**
		 * 批量删除指定轮播图
		 * localhost:8080/zhubaosheji/adminBanner?reqName=deleteBannerByIds
		 * @author 86176
		 *
		 */
			public void deleteBannerByIds(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
				String bIds =request.getParameter("bannerId");
				BannerDaoImpl bDao = new BannerDaoImpl();//创建轮播实体类
				int n = bDao.deleteBannerByIds(bIds);//调用删除方法
				
				response.setCharacterEncoding("UTF-8");
				PrintWriter out = response.getWriter();
				
				if (n>0) {//删除成功
					out.print(Result.toClient(true, "删除成功",""));//{"success":true,"message":"删除成功","remark":""}	
				} else {//删除失败
					out.print(Result.toClient(false, "删除失败",""));//{"success":false,"message":"删除失败","remark":""}	
				}
				
				out.flush();
				out.close();
			}
		/**
		 * 添加指定轮播图
		 * localhost:8080/zhubao/adminBanner?reqName=addBanner
		 * @author 86176
		 *
		 */
		public void addBanner(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			response.setCharacterEncoding("UTF-8");
			PrintWriter out = response.getWriter();
			BannerDaoImpl bDao = new BannerDaoImpl();//创建轮播实体类
			
			String bannerPicture =request.getParameter("picName");
			int bannerCategory =Integer.parseInt(request.getParameter("productCategoryId"));
			
			if(bannerPicture != null && !bannerPicture.equals("")&& bannerCategory>0) {
					Banner ba = new Banner(0, bannerPicture,bannerCategory);
					int n =bDao.addBanner(ba);//调用添加轮播图方法
					
					if (n>0) {//添加成功
						out.print(Result.toClient(true, "添加成功",""));//{"success":true,"message":"删除成功","remark":""}	
					} else {//添加失败
						out.print(Result.toClient(false, "添加失败",""));//{"success":false,"message":"删除失败","remark":""}	
					}
					
			}else {
				out.print(Result.toClient(false, "添加失败,图片或分类为空",""));//图片为空或分类为空
			}
			
			out.flush();
			out.close();
		}
		/**
		   *根据Id查询指定轮播图
		 * localhost:8080/zhubao/adminBanner?reqName=getBannerById
		 * @author 86176
		 *
		 */
		public void getBannerById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			response.setCharacterEncoding("UTF-8");
			PrintWriter out = response.getWriter();
			BannerDaoImpl bDao = new BannerDaoImpl();//创建轮播实体类
			
			int bannerId =Integer.parseInt(request.getParameter("bId"));//获取前台传来的参数             
			Banner baList = bDao.getBannerById(bannerId);//调用根据id查询的方法
			
			Gson gson= new Gson();//创建gson对象
			String res = gson.toJson(baList);//将baList转化为json字符串
			
			out.print(res);//响应数据
			System.out.println(res);
			
					
			out.flush();
			out.close();
		}
		/**
		 * 编辑指定轮播图
		 * localhost:8080/zhubao/adminBanner?reqName=editBanner
		 * @author 86176
		 *
		 */
		public void editBanner(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			String picName =request.getParameter("picName");//接收图片路径
			int productCategoryId =Integer.parseInt(request.getParameter("productCategoryId"));//接收轮播Id
			int bId = Integer.parseInt(request.getParameter("bannerId"));
			
			response.setCharacterEncoding("UTF-8");
			PrintWriter out = response.getWriter();
			BannerDaoImpl bDao = new BannerDaoImpl();//创建轮播实体类
			
					Banner ba = new Banner(bId, picName, productCategoryId);
					int n =bDao.editBanner(ba);//调用修改产品类别方法
					
					if (n>0) {//修改成功
						out.print(Result.toClient(true, "修改成功",""));//{"success":true,"message":"修改成功","remark":""}	
					} else {//修改失败
						out.print(Result.toClient(false, "修改失败",""));//{"success":false,"message":"修改失败","remark":""}	
					}
					
			
			out.flush();
			out.close();
		}
}
